<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	String path = request.getContextPath();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<script>
	function upgradeRule_check(after_selectObj,needPoint,deductionPoint){
		 if(!needPoint){
		 	alert("请输入累计消费积分");
		 	return false;
		 }
		 if(after_selectObj.val()==-1){
		 	alert("请选择升级后级别");
		 	return false;
		 }
		 if(!deductionPoint){
		 	alert("请输入扣除积分");
		 	return false;
		 }
		 var ex = /^\d+$/;
		if (!ex.test(needPoint)) {
			alert("累计积分只能输入整数");
			return false;
		} 
		if (!ex.test(deductionPoint)) {
			alert("扣除积分只能输入整数");
			return false;
		} 
		return true;
	}
	function upgradeRule_changeTable(key,new_selectObj,needPoint,deductionPoint) {
		var tr = $("#tab tr");
		var old_text =$("input[name='oldCardEntityVO.id']").val();
		var changeVal ="<tr class='' id="+key+" align='center'>"
						+"<td width='20%'><div>"+old_text+"</div></td>"
						+"<td width='20%'><div>"+needPoint+"</div></td>"
						+"<td width='20%'><div>"+new_selectObj.text()+"</div></td>"
						+"<td width='20%'><div>"+deductionPoint+"</div></td>"
						+"<td width='20%'><div><a class='btnDel' onclick='upgradeRule_deltr(this);'>删除</a></div></td>"
						+"</tr>";
		 if(tr.length>0){
		 	for(var i=0;i<tr.length;i++){
		 		var trId = $(tr[i]).attr("id");
		 		if(trId==key){
		 			var infoTd = tr[i];
		 			infoTd.innerHTML=changeVal;
		 			return;
		 		}
		 	}
		 }
		$("#tab").append(changeVal);  			
			     
	}
	
	function upgradeRule_deltr(obj){
		var id=$(obj).parents("tr").attr("id");
		$.ajax({cache:false, 
		url:"<%=path%>/ajax/deleteUpgradeRule.do?id="+id, 
		type:"post", 
		dataType:"json", 
		success:function(json){upgradeRule_deleteAjaxDone(json,obj);}});
	}
	
	function upgradeRule_deleteAjaxDone(json,obj){
		navTabAjaxDone(json);
			 if (json.statusCode == DWZ.statusCode.ok){
			 var rowCount =$("#tab tr").length;
			 if(rowCount>0)
				$(obj).parents("tr").remove(); 
		}
	}
	
	function upgradeRule_addAjaxDone(json,new_selectObj,needPoint,deductionPoint){
		navTabAjaxDone(json.result);
		 if (json.result.statusCode == DWZ.statusCode.ok){
			upgradeRule_changeTable(json.key,new_selectObj,needPoint,deductionPoint);
		}
	}

	function upgradeRule_ajaxsubmit(){
		var new_selectObj = $("select[name='upgradeRuleVO.newCardEntityId']").find("option:selected");
		var needPoint = $("input[name='upgradeRuleVO.needPoint']").val();
		var deductionPoint = $("input[name='upgradeRuleVO.deductionPoint']").val();
		if(upgradeRule_check(new_selectObj,needPoint,deductionPoint)){
			$.ajax({cache:false, 
			url:"<%=path%>/ajax/saveUpgradeRule.do?cardEntityId=${oldCardEntityVO.id}"
			+"&newCardEntityId="+new_selectObj.val()
			+"&needPoint="+needPoint
			+"&deductionPoint="+deductionPoint, 
			type:"post", 
			dataType:"json", 
			success:function(json){upgradeRule_addAjaxDone(json,new_selectObj,needPoint,deductionPoint)}});
		}
	}
	
</script>
	<head>
		<title>初始计次</title>
	</head>
	<body>
		<div class="pageContent">
			<form method="post" action="" class="pageForm required-validate">
				<div class="pageFormContent" layoutH="900">
					<p>
						<label>
							升级前级别：
						</label>
						<input type="text" name="oldCardEntityVO.id" value="【<s:property value="oldCardEntityVO.cardName"/>（<s:property value="oldCardEntityVO.cardLevelVO.level"/>）】" readonly>
					</p>
					<p>
						<label>
							累计消费积分：
						</label>
						<input name="upgradeRuleVO.needPoint"
							value="<s:property value="upgradeRuleVO.needPoint" />"
							class="required digits" type="text" size="30"/>
					</p>
					<p>
						<label>
							升级后级别：
						</label>
						<select name="upgradeRuleVO.newCardEntityId" >
							<option value="-1">请选择</option>
							<s:iterator value="cardEntityVOList" id="cardEntity" status="st">
								<option value="<s:property value="id" />">【<s:property value="cardName"/>（<s:property value="cardLevelVO.level"/>）】</option>
							</s:iterator>
	              		</select>
					</p>
					<p>
						<label>
							扣除积分：
						</label>
						<input name="upgradeRuleVO.deductionPoint"
							value="<s:property value="upgradeRuleVO.deductionPoint" />"
							class="required digits" type="text" size="30"/>
					</p>
				</div>
				<div class="formBar">
					<ul>
						<li>
							<div class="buttonActive">
								<div class="buttonContent">
									<button type="button" onclick="upgradeRule_ajaxsubmit();">
										增加
									</button>
								</div>
							</div>
						</li>
					</ul>
				</div>
				<table class="table" width="100%" layoutH="200">
					<thead>
						<tr>
							<th width="20%">
								升级前级别
							</th>
							<th width="20%">
								累计消费积分
							</th>
							<th width="20%">
								升级后级别
							</th>
							<th width="20%">
								扣除积分
							</th>
							<th width="70">
								操作
							</th>
						</tr>
					</thead>
					<tbody id="tab">
							<c:if test="${oldUpgradeRuleVO!=null}">
							<tr class="" align="center" id="<s:property value="oldUpgradeRuleVO.id" />">
								<td>
									【<s:property value="oldCardEntityVO.cardName"/>（<s:property value="oldCardEntityVO.cardLevelVO.level"/>）】
								</td>
								<td>
									<s:property value="oldUpgradeRuleVO.needPoint"/>
								</td>
								<td>
									<s:iterator value="cardEntityVOList" id="cardEntityVO" status="st">
										<c:if test="${cardEntityVO.id==oldUpgradeRuleVO.newCardEntityId}">
											【<s:property value="cardName"/>（<s:property value="cardLevelVO.level"/>）】
										</c:if>
									</s:iterator>
								</td>
								<td>
									<s:property value="oldUpgradeRuleVO.deductionPoint"/>
								</td>
								<td>
									<a class="btnDel" onclick="upgradeRule_deltr(this);">删除</a>
								</td>
							</tr>
						</c:if>
					</tbody>
				</table>
			</form>
		</div>
	</body>
</html>